package com.sdry.web.controller.permission;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sdry.entity.permission.UpmsStaff;
import com.sdry.entity.permission.UpmsStaffRole;
import com.sdry.service.permission.UpmsStaffService;
import com.sdry.utils.DateJsonValueProcessor;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;


/**
*
* @ClassName:UpmsStaffController
* @Description:员工
* @Author tdd
* @Date Mon Jul 30 15:22:45 CST 2018
* @version 1.0
*/
@Controller
@RequestMapping("/upmsStaff")
public class StaffController{
   /**注入员工Server*/
   @Resource UpmsStaffService  upmsStaffService;

    /**
     * 去员工信息页面
     * @return
     */
    @RequestMapping(value = "/baseInfoStaff", method = RequestMethod.GET)
    public String baseInfoCompany() {

        return "/gemini/info_staff";
    }

   /**
    * 条件分页查询
    * @param upmsStaff 员工实体类
    * @param response
    * @return
    * @throws Exception
    */
   @RequestMapping("/list")
   public String list(UpmsStaff upmsStaff,HttpServletResponse response) throws Exception{
   	/*
   	* 条件分页查询内容
   	*/
   	List<UpmsStaff> list = upmsStaffService.findPageByMution(upmsStaff);
   	/*
   	* 条件查询总条数
   	*/
   	Integer total = upmsStaffService.selectCountByMution(upmsStaff);

   	/*
   	* 转为json数据传至页面
   	*/
   	JsonConfig jsonConfig = new JsonConfig();
   	jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
   	//创建result
   	JSONObject result = new JSONObject();
   	//转json数据
   	JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
   	//添加入result
   	result.put("rows", jsonArray);
   	result.put("total", total);
   	//发送result
   	ResponseUtil.write(response, result);
   	return null;
   }
   /**
    * 添加员工
    * @param upmsStaff 员工实体类
    * @param response
    * @return
    * @throws Exception
   */
   @RequestMapping("/add")
   public void add(UpmsStaff upmsStaff, HttpServletRequest request, HttpServletResponse response) throws Exception{
       /*
        * 添加返回id
       */

       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
       upmsStaff.setSentrytime(df.format(new Date()));//给录入时间赋值
       //判断session
       HttpSession session  = request.getSession();
       //从session中取出用户身份信息
       UpmsStaffRole staffSession = (UpmsStaffRole) session.getAttribute("user");

       String name = null;
       if(staffSession != null){
           name = staffSession.getUpmsStaff().getSname();
       }
       upmsStaff.setSentry(name);

       Long id = upmsStaffService.insert(upmsStaff);
       if (null != id) {
           response.getWriter().print("1");
       }else{
           response.getWriter().print("0");
       }
   }
   /**
    * 多删除
    * @param ids 员工id累加字符串
    * @param session
    * @param response
    * @return
    * @throws Exception
   */
   @RequestMapping("/delete")
   public void delete(String ids,HttpSession session, HttpServletResponse response) throws Exception{
       System.out.println(ids);
       ids=ids.replace("'",""); 
       /*
        * 多删除返回影响行数
       */
       Integer count = upmsStaffService.delete(ids);
       if (count >= 1) {
           response.getWriter().print("1");
       }else{
           response.getWriter().print("0");
       }
   }
   /**
    * 条件查询员工
    * @param upmsStaff 员工实体类
    * @return 员工实体类集合
   */
   @RequestMapping("/queryAll")
   @ResponseBody
   public List<UpmsStaff> queryAllByMution(UpmsStaff upmsStaff){
       List<UpmsStaff> list = upmsStaffService.queryAllByMution(upmsStaff);
       return list;
   }
   /**
    * 根据id查员工
    * @param id 员工id
    * @return 员工实体类
   */
   @RequestMapping("/queryById")
   @ResponseBody
   public UpmsStaff queryById(Long id){
       UpmsStaff upmsStaff = upmsStaffService.queryById(id);
       return upmsStaff;
   }
   /**
    * 修改部门
    * @param upmsStaff 员工实体类
    * @param response
    * @return
    * @throws Exception
   */
   @RequestMapping("/update")
   public void update(UpmsStaff upmsStaff,HttpServletRequest request, HttpServletResponse response) throws Exception{
       /*
        * 修改返回影响行数
       */

       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
       upmsStaff.setSentrytime(df.format(new Date()));//给录入时间赋值
       //判断session
       HttpSession session  = request.getSession();
       //从session中取出用户身份信息
       UpmsStaffRole staffSession = (UpmsStaffRole) session.getAttribute("user");

       String name = null;
       if(staffSession != null){
           name = staffSession.getUpmsStaff().getSname();
       }
       upmsStaff.setSentry(name);
       Integer count = upmsStaffService.update(upmsStaff);
       if (count >= 1) {
           response.getWriter().print("1");
       }else{
           response.getWriter().print("0");
       }
   }
}
